package com.company.exp2.util.Recorder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class StandardTimeRecorder implements Recordable {

    // count表示容器中的数据包个数
    int count;

    // 记录存储键值对
    Map<Integer, Long> recordMap;

    // 当前状态开始时间
    long startTime;

    // 总时长
    long totalTime;

    // 结果集
    Map<Integer, Double> resultMap;

    {
        count = 0;
        recordMap = new HashMap<>();
        startTime = 0L;
        totalTime = 0L;
        resultMap = new HashMap<>();
    }

    @Override
    public synchronized void onPut() {
        // 记录上一个状态
        recordLastStatus();
        // 个数加一
        count ++;
    }

    @Override
    public synchronized void onPop() {
        // 记录上一个状态
        recordLastStatus();
        // 个数减一
        count --;
        if (count < 0) count = 0;
    }

    @Override
    public void log() {
        File file = new File("D:/A_3a/Computer_network/experiment/experiment1/code/src/com/company/mm1/output/output.txt");
        checkFileExistOrNot(file);
        generateResult();
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(printMap());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void checkFileExistOrNot(File file) {
        try {
            boolean isExisted = file.createNewFile();
            if (isExisted) {
                System.out.println("文件已存在, 已清空并重新创建");
                assert file.delete() && file.createNewFile();
            } else {
                System.out.println("文件已被创建");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void printOutFileContent(File file) {
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(printMap());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成分布函数图
    public void generateResult() {
        long accumulatedTime = totalTime;
        Set<Integer> integers = recordMap.keySet();
        for (int i : integers) {
            accumulatedTime -= recordMap.get(i);
            resultMap.put(i, (accumulatedTime + 0.0) / totalTime);
        }
    }

    private String printMap() {
        Set<Integer> integers = resultMap.keySet();
        StringBuilder result = new StringBuilder("{");
        for (int i : integers) {
            result.append(" \"").append(i).append("\"").append(" : ").append(resultMap.get(i)).append(",");
        }
        result.replace(result.length() - 1, result.length() + 1, "}");
        return result.toString();
    }

    private void recordLastStatus() {
        if (startTime == 0) {
            // 第一次记录
            startTime = getTime();
        } else {
            // 获取结束时间
            long endTime = getTime();

            // 获取时间差
            long deltaTime = endTime - startTime;

            // 积累时差
            totalTime += deltaTime;

            // 更新新的状态起始时间
            startTime = endTime;

            // 将时间差记录入map
            if (recordMap.containsKey(count)) {
                recordMap.put(count, recordMap.get(count) + deltaTime);
            } else {
                recordMap.put(count, deltaTime);
            }
        }
    }

    private long getTime() {
        return new Date().getTime();
    }

}
